package cn.itsource.product.mapper;

import cn.hutool.core.util.StrUtil;
import cn.itsource.basic.domain.AjaxResult;
import cn.itsource.basic.mysuper.MyBaseMapper;
import cn.itsource.basic.util.PageList;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMessageQuery;
import cn.itsource.pet.domain.StateIdsDto;
import cn.itsource.product.domain.Product;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public interface ProductMapper extends MyBaseMapper<Product> {
    //这个方法就是获取当前实体类的泛型大的class
    default Class<Product> getTClass(){
        return Product.class;
    };

    default PageList pagelist(SearchMasterMessageQuery query){
        PageHelper.startPage(query.getPage(),query.getPageSize());
        Example example = new Example(getTClass());
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("del", 1);

        if (StrUtil.isNotBlank(query.getKeyword())){
            criteria.andLike("name","%"+ query.getKeyword()+"%");
        }
        //如果状态不为空
        if (Objects.nonNull(query.getState())){
            criteria.
                    andEqualTo("state",query.getState());
        }
        //如果时间不为空
        if (StrUtil.isNotBlank(query.getBetweenTime())){
            String[] split = query.getBetweenTime().split(",");
            criteria.
                    andBetween("createTime",split[0],split[1]);
        }
        List<Product> products = this.selectByExample(example);
        PageInfo<Product> pageInfo = new PageInfo<>(products);

        return new PageList<>(pageInfo.getTotal(), pageInfo.getList());
    };

    default AjaxResult upbutton(StateIdsDto stateIdsDto , Product product){
        Example example = new Example(getTClass());
        example.createCriteria()
                .andIn("id", Arrays.asList(stateIdsDto.getIds()));
        product.setState(stateIdsDto.getState());
        this.updateByExampleSelective(product, example);
        return AjaxResult.me();
    };

    default PageList findAll(SearchMasterMessageQuery query){
        PageHelper.startPage(query.getPage(),query.getPageSize());
        Example example = new Example(getTClass());
        example.createCriteria()
                .andEqualTo("del", 1);
        List<Product> products = this.selectByExample(example);
        PageInfo<Product> pageInfo = new PageInfo<>(products);

        return new PageList<>(pageInfo.getTotal(), pageInfo.getList());
    };

    @Select("select * FROM t_product WHERE id = #{id}")
    Product proFindOne(Long id);
}
